package com.gin.kotlin.no_gay_alliance.provider.ngaphp

import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.model.PrivateMessageListModel
import com.gin.nga.main.NgaApi
import com.gin.nga.response.message.PriMessageList
import java.lang.ref.WeakReference

class PrivateMessageListProvider private constructor(): AbstractCachedDataProvider<Int, List<PriMessageList>, PrivateMessageListModel>(
    javaType = TYPE_FACTORY.constructType(PrivateMessageListModel::class.java)
) {
    override val dir = "PRIVATE_MESSAGE_LIST"

    override fun transform(rawData: List<PriMessageList>) = rawData.takeIf { it.isNotEmpty() }?.get(0)?.let { PrivateMessageListModel(it) } ?: PrivateMessageListModel()

    override fun requestMethod(ngaApi: NgaApi, params: Int?) = App.INSTANCE.ngaApi.messageApi.list(params ?: 1)

    override fun obtainRelativePath(userId: Long, params: Int?) = "${userId}_${params}.json"

        companion object {
            private var instance: WeakReference<PrivateMessageListProvider> = WeakReference(PrivateMessageListProvider())
            fun getInstance(): PrivateMessageListProvider = instance.get() ?: PrivateMessageListProvider().also { instance = WeakReference(it) }
        }
}